using System.IO;

namespace TimeMachine.Engine.Vcs.Svn
{
	public interface ISvnLogLoaderSaver
	{
		byte[] Load();
		void Save(byte[] bytesToSave);
	}

	public class SvnLogLoaderSaver : ISvnLogLoaderSaver
	{
		private readonly string filepath;

		public SvnLogLoaderSaver(IProjectSettings projectSettings)
			: this(projectSettings.StoragePath, projectSettings.Name)
		{
		}

		internal SvnLogLoaderSaver(string storagePath, string projectName)
		{
			string dirPath = Path.Combine(storagePath, projectName);
			Directory.CreateDirectory(dirPath);
			filepath = Path.Combine(dirPath, "log.xml");
		}

		public byte[] Load()
		{
			return File.Exists(filepath) ? File.ReadAllBytes(filepath) : null;
		}

		public void Save(byte[] bytesToSave)
		{
			File.WriteAllBytes(filepath, bytesToSave);
		}
	}
}